
import React, { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { Collapse, Spin, Tree } from "antd";
import { DevTree, LayoutB, DevPopup } from '~/public/cpas-ui';
// import Accordion, { AccordionTypes } from 'devextreme-react/accordion';
import type { DataNode } from 'antd/es/tree';
import './index.less';
// import RightTables from "./rightTables";
import { DatabaseOutlined, DatabaseTwoTone, FrownFilled, FrownOutlined, ScheduleOutlined, ScheduleTwoTone } from "@ant-design/icons";
// import { getAccountInfo } from "./service";
import { useModel } from "umi";
import PageLoading from "../components/PageLoading";
import { msgInfo } from "@/utils/msg";
import TableSamplingRecord from "./tableSamplingRecord";
import TableSamplingList from "./tableSamplingList";

const { Panel } = Collapse;

// 创建一个 Context 对象
const AuditSampTreeSelectKeyContext = React.createContext({});

const AuditSampling = () => {


    const { initialState } = useModel('@@initialState');
    const { dbname, sjnd } = initialState?.curPart as any;

    const [treeSelecNode, setTreeSelecNode] = useState<any>({
        treeSelectKey: 'samplingRecord',
        treeSelectName: '抽样记录',
        tableYearArr: [],
        spinLoad: false,
        yearID: sjnd
    });

    const [selectedItems1, setSelectedItems1] = useState(['samplingRecord']);

    // const getData = async () => {
    //     const res: any = await getAccountInfo({
    //         dbname,
    //     });
    //     if (res.success) {
    //         setTreeSelecNode((oldData: any) => {
    //             let newData: any = { ...oldData }
    //             newData.tableYearArr = res.data;
    //             return newData;
    //         });
    //     }
    // }
    // useEffect(() => {
    //     getData();
    // }, [])

    const samplingArr: DataNode[] = [
        {
            title: '抽样记录',
            key: 'samplingRecord',
            icon: <ScheduleOutlined />,
            style: { marginTop: 5 }
        },
        {
            title: '抽样清单',
            key: 'samplingList',
            icon: <DatabaseOutlined />,
            style: { marginTop: 5 }
        }
    ];

    const treeSel1 = (selectedKeys: any[], e: any) => {
        const { node } = e;
        if (selectedKeys.length > 0) {
            setSelectedItems1(selectedKeys);
            setTreeSelecNode((oldData: any) => {
                let newData: any = { ...oldData };
                newData.treeSelectKey = selectedKeys[0];
                newData.treeSelectName = node.title;
                return newData;
            });
        }

    }

    function leftContainer() {
        return (
            <div style={{ marginRight: "15px", height: "100%" }} className="AuditSamplingLeftCon">
                <Tree
                    className="noneSwitcherIcon"
                    // switcherIcon={false}
                    blockNode
                    treeData={samplingArr}
                    onSelect={treeSel1}
                    showIcon={true}
                    selectedKeys={selectedItems1}
                />
            </div>
        );
    }

    // 表格结构
    function rightContainer() {

        return (
            <div style={{ marginLeft: "15px", height: "100%" }} className="AuditSamplingRightCon">

                {selectedItems1[0] === 'samplingRecord' ? <TableSamplingRecord /> : <TableSamplingList />}
            </div>
        )
    }

    return (
        <div style={{ height: "100%" }}>
            <AuditSampTreeSelectKeyContext.Provider value={{ treeSelecNode, setTreeSelecNode }}>
                <PageLoading
                    isOpen={treeSelecNode.spinLoad}
                    isMaskClose={false}
                    content={"正在获取数据..."}
                    width={200}
                />
                <LayoutB
                    rowSize={["100px", "150px"]}
                    leftSlot={leftContainer()}
                    rightSlot={rightContainer()}
                />
            </AuditSampTreeSelectKeyContext.Provider>
        </div>
    )
}
export default AuditSampling;
export { AuditSampTreeSelectKeyContext }; // 必须这样写 重要！